import API from 'libs/api'
import nock from 'nock'

afterEach(() => {
  nock.cleanAll()
})

it('should call API.get right', () => {
  nock('http://localhost:90909090')
    .get('/api/events')
    .query(true)
    .reply(200, {})

  return API.get('http://localhost:90909090/api/events')
})

it('should handle API.get right when the response was not successful(status not in the range **200-299**)', () => {
  const errors = ['error1', 'error2']

  nock('http://localhost:90909090')
    .get('/api/events')
    .query(true)
    .reply(400, { errors })

  return API.get('http://localhost:90909090/api/events').catch(requestError => {
    expect(requestError.status).toBe(400)
    expect(requestError.statusText).toBe('Bad Request')
    expect(requestError.errors).toMatchObject(errors)

    expect(requestError).toMatchSnapshot()
  })
})

it('should call API.post right', () => {
  nock('http://localhost:90909090').post('/api/events').reply(200, {})

  return API.post('http://localhost:90909090/api/events')
})

it('serereer', () => {
  nock('http://localhost:90909090').post('/api/events').reply(500, {})

  return API.post(
    'http://localhost:90909090/api/evesnts',
  ).catch(requestError => {
    expect(requestError).toMatchSnapshot()
  })
})

it('should handle `error` right', () => {
  const error = ['error1', 'error2']

  nock('http://localhost:90909090')
    .get('/api/events')
    .query(true)
    .reply(401, { error })

  return API.get('http://localhost:90909090/api/events').catch(requestError => {
    expect(requestError.status).toBe(401)
    expect(requestError.statusText).toBe('Unauthorized')
    expect(requestError.errors).toMatchObject(error)

    expect(requestError).toMatchSnapshot()
  })
})
